<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
 * 图片轮播
 * 
 * @package ImageGallery
 * @author 梦千公
 * @version 1.0.0
 * @link http://www.xianhall.com
 */
class ImageGallery_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Typecho_Plugin::factory('Widget_Archive')->header = array('ImageGallery_Plugin', 'headlink');
        Typecho_Plugin::factory('Widget_Archive')->footer = array('ImageGallery_Plugin', 'footerlink');
        Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('ImageGallery_Plugin', 'publish');
        Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('ImageGallery_Plugin', 'publish');
    }
    
    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){

    }
    
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        /** 分类名称 */
        // $name = new Typecho_Widget_Helper_Form_Element_Text('word', NULL, 'Hello World', _t('说点什么'));
        // $form->addInput($name);
    }
    
    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}
    
    /**
     * 插件实现方法
     * 
     * @access public
     * @return void
     */
    public static function publish($content, $widget, $lastResult)
    {
        $Archive = Typecho_Plugin::factory('Widget_Archive');

        $content = empty($lastResult) ? $content : $lastResult;

        if ( $widget instanceof Widget_Archive && $Archive->is('index') || $Archive->is('post') || $Archive->is('archive') ) {
            $photoUrl = '/<img\s*src="([^"]+).(bmp|gif|jpeg|jpg|png)"(\s*alt[\s\S]+?\>)/i';

            //$replace = '<a href="$1.$2" data-gallery=""><img src="$1.$2-rec" $3 </a>';
            $replace = '<div class="thumbnail col-sm-3"><a href="$1.$2" data-gallery=""><img src="$1.$2" $3 </a></div>';
            $content = preg_replace('/<img\s*src="([^"]+).(bmp|gif|jpeg|jpg|png)"(\s*alt[\s\S]+?\>)/', $replace, $content);
            //去除br
            $content =  preg_replace('/(\s*<br[ ]*\/?>\s*)*|(\s*<br[ ]*\/?>\s*)/', '', $content);
        }
        return $content;
    }

    /**
     * 头部引用
     * 
     * @access public
     * @return void
     */
    public static function headlink()
    {
        $cssUrl = '<link href="//cdn.bootcss.com/blueimp-gallery/2.16.0/css/blueimp-gallery.min.css" rel="stylesheet">';

        $html = '<div id="blueimp-gallery" class="blueimp-gallery">';
        $html .= '<div class="slides"></div>';
        $html .= '<h3 class="title"></h3>';
        $html .= '<a class="prev">‹</a>';
        $html .= '<a class="next">›</a>';
        $html .= '<a class="close">×</a>';
        $html .= '<a class="play-pause"></a>';
        $html .= '<ol class="indicator"></ol>';
        $html .= '</div>';

        echo $cssUrl,$html;
    }

    /**
     * 底部引用
     * 
     * @access public
     * @return void
     */
    public static function footerlink()
    {
        $link1 = '<script src="//cdn.bootcss.com/blueimp-gallery/2.16.0/js/blueimp-helper.js"></script>';
        $link2 = '<script src="//cdn.bootcss.com/blueimp-gallery/2.16.0/js/blueimp-gallery.min.js"></script>';
        $link3 = '<script src="//cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script>';
        $link4 = '<script src="//cdn.bootcss.com/blueimp-gallery/2.16.0/js/jquery.blueimp-gallery.min.js"></script>';
        echo $link1,$link2,$link3,$link4;
    }
}
